package com.oocl.hw1.task3.controller.impl;

import java.util.Comparator;
import java.util.Iterator;

import com.oocl.hw1.task3.controller.PatchDataProcessable;
import com.oocl.hw1.task3.controller.Scannable;
import com.oocl.hw1.task3.model.DBConnection;
import com.oocl.hw1.task3.model.impl.CustomerDBManager;
import com.oocl.hw1.task3.pojo.Customer;
import com.oocl.hw1.task3.view.ViewDelegate;

public class CustomerScanner implements Scannable<Customer> {
	private ViewDelegate mView;
	private DBConnection<Customer> dbConnecton;
	private PatchDataProcessable<Customer> patchDataProcessable;

	public CustomerScanner(ViewDelegate view,
			PatchDataProcessable<Customer> patchDataProcessable) {
		this.mView = view;
		this.patchDataProcessable = patchDataProcessable;
		this.dbConnecton = new CustomerDBManager();
	}

	@Override
	public void scanString(String s) {

		// /具体拆分
		String[] strs = s.split("\\s");
		if (patchDataProcessable != null) {
			Customer customer = patchDataProcessable.stringToObject(strs);
			if (this.dbConnecton != null) {
				this.dbConnecton.add(customer);
			}
		}
	}

	/**
	 * 默认： 按照id排序
	 */
	public void getDataOrderById() {
		// /开始拿出数据
		// /默认排序
		Iterator<Customer> it = this.dbConnecton.get(null);
		StringBuilder sb = new StringBuilder();
		sb.append("id------------name-------------email-------------sex-------------birthday\n");
		while (it.hasNext()) {
			Customer customer = it.next();
			String[] values = this.patchDataProcessable
					.objectToString(customer);
			sb.append(values[0]).append("          ").append(values[1])
					.append("          ").append(values[2])
					.append("          ").append(values[3])
					.append("          ").append(values[4])
					.append("          ").append("\n");
		}
		mView.showResultTips(sb.toString());
	}
	
	/**
	 * 按照  birthday 排序
	 */
	public void getDataOrderByBirthday(){
		///按照  birthday 排序
		Comparator<Customer> birthdayComparator = new Comparator<Customer>() {

			@Override
			public int compare(Customer o1, Customer o2) {
				return (o1.getBirthday().before(o2.getBirthday())) ? -1 : 1;
			}
		};
		Iterator<Customer> it = this.dbConnecton.get(birthdayComparator);
		StringBuilder sb = new StringBuilder();
		sb.append("id------------name-------------email-------------sex-------------birthday\n");
		while (it.hasNext()) {
			Customer customer = it.next();
			String[] values = this.patchDataProcessable
					.objectToString(customer);
			sb.append(values[0]).append("          ").append(values[1])
					.append("          ").append(values[2])
					.append("          ").append(values[3])
					.append("          ").append(values[4])
					.append("          ").append("\n");
		}
		mView.showResultTips(sb.toString());
	}

	
	/**
	 * 以xml形式输出
	 */
	public void getXMLDatas() {
		// /开始拿出数据
		Iterator<Customer> it = this.dbConnecton.get(null);
		StringBuilder sb = new StringBuilder();
		sb.append("-------------------XML形式----------------------------\n");
		sb.append("<customers>\n");
		while (it.hasNext()) {
			Customer customer = it.next();
			String[] values = this.patchDataProcessable
					.objectToString(customer);

			sb.append("\t<customer id=\"").append(values[0]).append("\">\n")
					.append("\t\t<name>").append(values[1]).append("</name>\n")
					.append("\t\t<email>").append(values[2])
					.append("</email>\n").append("\t\t<sex>").append(values[3])
					.append("</sex>\n").append("\t\t<birthday>")
					.append(values[4]).append("</birthday>\n")
					.append("\t</customer>\n");
		}

		sb.append("</customers>\n");

		mView.showResultTips(sb.toString());

	}

}
